package com.ttuproject.ttu.controller;

import com.ttuproject.ttu.advice.ResponseEnvelope;
import com.ttuproject.ttu.dao.User;
import com.ttuproject.ttu.exception.Advice;
import com.ttuproject.ttu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(value = "用户登录接口", tags = "用户登录接口", description = "用户登录接口")
public class UserController {


    @Autowired
    UserService userService;

   /**
       * @explain:   用户登录接口，返回值为1说明为管理员，返回值为2说明为普通用户
       * @author: cw
       * @date: 2021/12/1
           */
    //@RequestMapping("/user/login")
    @GetMapping("/login")
    @ApiOperation(value = "登陆")
    public ResponseEntity<ResponseEnvelope<Integer>> login(@RequestParam("userName") String userName,@RequestParam("passWord") String passWord) {
        return ResponseEntity.ok(new ResponseEnvelope<>(Advice.LOGIN_SUCCESS,userService.Userlogin(userName, passWord)) );
    }

    //@RequestMapping("/user/register")
    @GetMapping("/register")
    @ApiOperation("注册")
    public ResponseEntity<Boolean> register(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
        return ResponseEntity.ok(userService.register(userName, passWord));
    }

    //@RequestMapping("/user/update")
    @GetMapping("/update")
    @ApiOperation("修改")
    public ResponseEntity<ResponseEnvelope<Boolean>> update(@RequestBody User user,@RequestParam("userName") String userName,@RequestParam("passWord") String passWord) {
        return ResponseEntity.ok(new ResponseEnvelope<>(Advice.LOGIN_ERROR, userService.update(user,userName,passWord)));
    }


}
